一些代码样式工具推荐这个,我记得看到一些unix命令行工具警告缺少空行.
有多余空行的原因是什么?
我有一个CSV文件(24.1 MB),我无法完全读入我的R会话.当我在电子表格程序中打开文件时,我可以看到112,544行.当我将它读入R时,read.csv我只得到56,952行并且这个警告:
cit <- read.csv("citations.CSV", row.names = NULL,
comment.char = "", header = TRUE,
stringsAsFactors = FALSE,
colClasses= "character", encoding= "utf-8")
Warning message:
In scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, :
EOF within quoted string
Run Code Online (Sandbox Code Playgroud)
我可以将整个文件读入R readLines:
rl <- readLines(file("citations.CSV", encoding = "utf-8"))
length(rl)
[1] 112545
Run Code Online (Sandbox Code Playgroud)
但我不能把它作为一个表(通过read.csv)回到R中:
write.table(rl, "rl.txt", quote = FALSE, row.names = FALSE)
rl_in <- read.csv("rl.txt", skip = 1, row.names = NULL)
Warning message:
In scan(file, what, nmax, …Run Code Online (Sandbox Code Playgroud) 要读取一些文本文件,在C或Pascal中,我总是使用以下代码段来读取数据,直到EOF:
while not eof do begin
readline(a);
do_something;
end;
Run Code Online (Sandbox Code Playgroud)
因此,我想知道如何在Python中简单快速地完成这项工作?
这是我的python代码.有人能告诉我它有什么问题.
while 1:
date=input("Example: March 21 | What is the date? ")
if date=="June 21":
sd="23.5° North Latitude"
if date=="March 21" | date=="September 21":
sd="0° Latitude"
if date=="December 21":
sd="23.5° South Latitude"
if sd:
print sd
Run Code Online (Sandbox Code Playgroud)
这是发生的事情:
>>>
Example: March 21 | What is the date?
Traceback (most recent call last):
File "C:\Users\Daniel\Desktop\Solar Declination Calculater.py", line 2, in <module>
date=input("Example: March 21 | What is the date? ")
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
>>>
Run Code Online (Sandbox Code Playgroud) 我目前正在阅读Ritchie&Kernighan的C Programming Language一书.而且我对getchar()函数中EOF的使用感到非常困惑.
首先,我想知道为什么EOF的值是-1,为什么值为getchar()!=EOF0.请原谅我的问题,但我真的不明白.我真的尝试过,但我做不到.
然后我尝试在书上运行可以使用下面的代码计算字符数的例子,但似乎即使按下回车我也永远不会离开循环所以我想知道我什么时候才能达到EOF?
main(){
long nc;
nc = 0;
while (getchar() != EOF)
++nc;
printf("%ld\n", nc);
}
Run Code Online (Sandbox Code Playgroud)
然后,我在C中的EOF问题中读到了同样的问题.大多数人建议不要使用EOF,而是使用终结符\n或空终结符'\ 0',这很有意义.
这是否意味着书中的例子有另一个目的?
fp = open("a.txt")
#do many things with fp
c = fp.read()
if c is None:
print 'fp is at the eof'
Run Code Online (Sandbox Code Playgroud)
除了上面的方法,任何其他方式来找出是否是fp已经在eof?
我目前正在阅读K&R的书并在第一部分中输入示例,并且有几个例子如下:
while((c = getchar()) != EOF) {
//do something
}
Run Code Online (Sandbox Code Playgroud)
我正在Windows框中测试这些示例,从而从cmd提示符运行已编译的exe文件.
要测试上面的例子,我该如何模拟EOF?也就是说,从命令提示符测试示例时,基本上如何使循环停止?
无法弄清楚,在PyCharm中改变EOF的位置.我的脚本,以:
#!/usr/bin/python
# -*- coding: utf-8 -*-
Run Code Online (Sandbox Code Playgroud)
输出类似这样的东西,当我尝试像可执行文件(chmode + x)一样运行它时:
-bash:./ main.py:/ usr/bin/python ^ M:错误的解释器:没有这样的文件或目录
该怎么做以及如何做?
假设我有以下Bash脚本:
while read SCRIPT_SOURCE_LINE; do
echo "$SCRIPT_SOURCE_LINE"
done
Run Code Online (Sandbox Code Playgroud)
我注意到,对于最后没有换行的文件,这将有效地跳过最后一行.
我一直在寻找解决方案,并发现了这个:
当读取到达文件结尾而不是行尾时,它会读入数据并将其分配给变量,但它会以非零状态退出.如果你的循环是"在阅读时构建的;做的事情;完成
因此,不是直接测试读取退出状态,而是测试一个标志,并让read命令在循环体内设置该标志.这样,无论读取退出状态如何,整个循环体都会运行,因为读取只是循环中的命令列表之一,就像其他任何循环一样,而不是决定循环是否会运行的决定因素.
Run Code Online (Sandbox Code Playgroud)DONE=false until $DONE ;do read || DONE=true # process $REPLY here done < /path/to/file.in
如何重写此解决方案,使其行为与while我之前的循环完全相同,即没有硬编码输入文件的位置?
我使用PHP的EOF字符串来格式化HTML内容,而不必担心必须转义引号等.如何在此字符串中使用该函数?
<?php
$str = <<<EOF
<p>Hello</p>
<p><?= _("World"); ?></p>
EOF;
echo $str;
?>
Run Code Online (Sandbox Code Playgroud) eof ×10
python ×4
c ×2
file ×2
bash ×1
coding-style ×1
csv ×1
getchar ×1
gettext ×1
interpreter ×1
iteration ×1
newline ×1
php ×1
pycharm ×1
python-2.x ×1
r ×1
read.table ×1